package com.ruoyi.product.controller;

import java.util.List;

import com.ruoyi.product.domain.SpuSkuVo;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.product.domain.ProductSpu;
import com.ruoyi.product.service.IProductSpuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品SPUController
 * 
 * @author liuhuizhong
 * @date 2025-08-07
 */
@RestController
@RequestMapping("/product/productSpu")
public class ProductSpuController extends BaseController
{
    @Autowired
    private IProductSpuService productSpuService;

    /**
     * 根据SPU ID查询SPU及关联的SKU信息
     */
    @GetMapping("/{spuId}/withSku")
    public AjaxResult getSpuWithSku(@PathVariable Long spuId) {
        List<SpuSkuVo> result = productSpuService.getSpuWithSkuBySpuId(spuId);
        System.out.println("getSpuWithSkuBySpuId"+ result);
        return AjaxResult.success(result);
    }

    /**
     * 查询商品SPU列表
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:list')")
    @GetMapping("/list")
    public TableDataInfo list(ProductSpu productSpu)
    {
        startPage();
        List<ProductSpu> list = productSpuService.selectProductSpuList(productSpu);
        return getDataTable(list);
    }

    /**
     * 导出商品SPU列表
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:export')")
    @Log(title = "商品SPU", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ProductSpu productSpu)
    {
        List<ProductSpu> list = productSpuService.selectProductSpuList(productSpu);
        ExcelUtil<ProductSpu> util = new ExcelUtil<ProductSpu>(ProductSpu.class);
        util.exportExcel(response, list, "商品SPU数据");
    }

    /**
     * 获取商品SPU详细信息
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(productSpuService.selectProductSpuById(id));
    }

    /**
     * 新增商品SPU
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:add')")
    @Log(title = "商品SPU", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ProductSpu productSpu)
    {
        return toAjax(productSpuService.insertProductSpu(productSpu));
    }

    /**
     * 修改商品SPU
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:edit')")
    @Log(title = "商品SPU", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ProductSpu productSpu)
    {
        return toAjax(productSpuService.updateProductSpu(productSpu));
    }

    /**
     * 删除商品SPU
     */
    @PreAuthorize("@ss.hasPermi('product:productSpu:remove')")
    @Log(title = "商品SPU", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(productSpuService.deleteProductSpuByIds(ids));
    }
}
